$cat = {
	getCategoryName : function(id) {
		var select = $('#category-list');
		return select.find('option[value="' + id + '"]').text();
	},
	getParentName : function(id) {
		var select = $('#var-list');
		return select.find('option[value="' + id + '"]').text();
	},
	loadCategory : function(args) {
		$store.get({
			url : '/admin/variation/load/category',
			data : null,
			success : function(d) {
				var select = $('#category-list');
				for ( var i = 0; i < d.length; i++) {
					var c = d[i];
					option = $('<option/>');
					option.val(c.id).text(
							c.parentId === -1 ? c.name : '+' + c.name);
					select.append(option);
				}
				args.onLoad();
			},
			error : function(e) {
				alert('error loading categories');
			}
		});
	},
	load : function() {
		$store.get({
			url : '/admin/variation/load',
			data : null,
			success : function(d) {
				var select = $('#var-list');
				select.children().remove();
				var option = $('<option/>');
				option.val(-1).text('[none]');
				select.append(option);

				// refresh the table
				$('#var-table').remove();

				var table = $('#var-table-template').clone();
				table.attr('id', 'var-table');
				table.css('display', 'block').appendTo('#page-root');
				for ( var i = 0; i < d.length; i++) {
					var c = d[i];

					option = $('<option/>');
					option.val(c.id).text(
							c.parentId === -1 ? c.name : '+' + c.name);
					select.append(option);

					var row = table.find('tr.var-row:first').clone();
					row.removeClass('hide');
					table.append(row);
					row.find('td::nth-child(1)').text(c.id);
					row.find('td:nth-child(2)').text(c.name);
					row.find('td:nth-child(3)').text(c.description);
					row.find('td:nth-child(4)').data('category-id',
							c.categoryId);
					row.find('td:nth-child(5)').data('parent-id', c.parentId);
					row.find('td:nth-child(4)').text(
							$cat.getCategoryName(c.categoryId));
					row.find('td:nth-child(5)').text(
							$cat.getParentName(c.parentId));
				}
				table.find('tr:odd').css('background-color', '#cabcab');
				$('a[title="edit"]').click(
						function() {
							var $this = $(this).closest('tr');
							$('#var-id').val(
									$this.find('td:nth-child(1)').text());
							$('#var-name').val(
									$this.find('td:nth-child(2)').text());
							$('#category-list').val(
									$this.find('td:nth-child(4)').data(
											'category-id'));
							$('#var-list').val(
									$this.find('td:nth-child(5)').data(
											'parent-id'));
							$('#var-desc').val(
									$this.find('td:nth-child(3)').text());
						});
			},
			error : function(e) {
				alert('error loading variations');
			}
		});
	},
	save : function() {
		var form = $('#var-form');
		$store.post({
			url : '/admin/variation/save',
			data : form.serialize(),
			success : function(d) {
				alert('done');
				$('#var-id').val(null);
				$cat.load(false);
			},
			error : function(e) {
				alert('error');
			}
		});
	}
};
$(document).ready(function() {
	$cat.loadCategory({
		onLoad : function() {
			$cat.load();
		}
	});
	$('#save-var-button').click(function() {
		$cat.save();
	});
	$('#reset-var-button').click(function() {
		$('#category-id').val(null);
	});
});